﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace LibExtend.GlassHelpers
{
    public class Helpers
    {
        public enum Corner
        {
            None = 0,
            TopLeft = 1,
            TopRight = 2,
            BottomLeft = 4,
            BottomRight = 8,
            All = TopLeft | TopRight | BottomLeft | BottomRight,
            AllTop = TopLeft | TopRight,
            AllLeft = TopLeft | BottomLeft,
            AllRight = TopRight | BottomRight,
            AllBottom = BottomLeft | BottomRight
        }

        public static GraphicsPath RoundCorners(RectangleF Rectangle, int Radius, Corner Corners)
        {
            GraphicsPath p = new GraphicsPath();
            Single x = Rectangle.X;
            Single y = Rectangle.Y;
            Single w = Rectangle.Width;
            Single h = Rectangle.Height;
            int r = Radius;

            p.StartFigure();
            //top left arc
            if ((Corners & Corner.TopLeft)==Corner.TopLeft)
                p.AddArc(new RectangleF(x, y, 2 * r, 2 * r), 180, 90);
            else
            {
                p.AddLine(new PointF(x, y + r), new PointF(x, y));
                p.AddLine(new PointF(x, y), new PointF(x + r, y));
            }

            //top line
            p.AddLine(new PointF(x + r, y), new PointF(x + w - r, y));

            //'top right arc
            if((Corners & Corner.TopRight) == Corner.TopRight)
                p.AddArc(new RectangleF(x + w - 2 * r, y, 2 * r, 2 * r), 270, 90);
            else
            {
                p.AddLine(new PointF(x + w - r, y), new PointF(x + w, y));
                p.AddLine(new PointF(x + w, y), new PointF(x + w, y + r));
            }

            //'right line
            p.AddLine(new PointF(x + w, y + r), new PointF(x + w, y + h - r));

            //'bottom right arc
            if ((Corners & Corner.BottomRight) == Corner.BottomRight)
                p.AddArc(new RectangleF(x + w - 2 * r, y + h - 2 * r, 2 * r, 2 * r), 0, 90);
            else
            {
                p.AddLine(new PointF(x + w, y + h - r), new PointF(x + w, y + h));
                p.AddLine(new PointF(x + w, y + h), new PointF(x + w - r, y + h));
            }

            //'bottom line
            p.AddLine(new PointF(x + w - r, y + h), new PointF(x + r, y + h));

            //'bottom left arc
            if((Corners & Corner.BottomLeft) == Corner.BottomLeft)
                p.AddArc(new RectangleF(x, y + h - 2 * r, 2 * r, 2 * r), 90, 90);
            else
            {
                p.AddLine(new PointF(x + r, y + h), new PointF(x, y + h));
                p.AddLine(new PointF(x, y + h), new PointF(x, y + h - r));
            }

            //'left line
            p.AddLine(new PointF(x, y + h - r), new PointF(x, y + r));

            //'close figure...
            p.CloseFigure();

            return p;
        }

        public static int Check(int Value)
        {
            if( Value > 255)
                return 255;
            else if( Value < 0)
                return 0;
            else
                return Value;
        }

        public Color Darken(Color Color)
        {
            return Color.FromArgb(Color.A, DeductMinZero(Color.R, 20), DeductMinZero(Color.G, 20), DeductMinZero(Color.B, 20));
        }

        public Color Lighten(Color Color)
        {
            return Color.FromArgb(Color.A, AddMax255(Color.R, 20), AddMax255(Color.G, 20), AddMax255(Color.B, 20));
        }

        public static int DeductMinZero(int Value, int Deduction)
        {
            if (Value - Deduction < 0)
                return 0;
            else
                return Value - Deduction;
        }

        public int AddMax255(int Value, int Addition)
        {
            if( Value + Addition > 255 )
                return 255;
            else
                return Value + Addition;
        }
    }
}
